<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\CustomerProductRequest;
use App\Models\CustomerProduct;
use App\Models\CustomerProductChannel;
use App\Models\SysUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CustomerProductController extends ApiBaseController
{
    public function index(Request $request)
    {

        $paginator = CustomerProduct::with(['product', 'user', 'spec', 'channel_group', 'channels.channel'])->filter($request->all())->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function show(Request $request, CustomerProduct $model)
    {
        $id = $request->get('id');
        $data = $model->with(['product', 'spec', 'channels.channel'])->find($id);
        return success(['data' => $data->toArray()]);
    }
    //批量修改客户产品折扣
    public function changeUserDiscount(Request $request)
    {
        $params = $request->only(['ids', 'discount']);
        if (!isset($params['ids']) || !isset($params['discount'])) {
            return error('ids 或者discount 不能为空');
        }
        if ($params['discount'] > 100) {
            return error('折扣不能大于100');
        }
        // 
        foreach ($params['ids'] as $key => $value) {
            //查询价格
            $info = CustomerProduct::with(['spec'])->find($value);
            //原价
            $oldPrice = $info->spec->market_price;
            $newPrice = $oldPrice * $params['discount'] / 100;
            $info->user_price = $newPrice;
            $info->save();
        }
        return success();
    }
    public function addProduct(CustomerProductRequest $request)
    {
        $params = $request->all();
        DB::transaction(function () use ($params) {
            $items = $params['items'];
            $channels = $params['channels'];

            $all_user_ids = $params['user_ids'];
            if (isset($params['submit_order_count']) && $params['submit_order_count']) {
                $submit_order_count = $params['submit_order_count'];
            } else {
                $submit_order_count = env('ORDER_RE_SUBMIT_LIMIT');
            }
            //全部客户
            if (count($all_user_ids) == 1 && $all_user_ids[0] == 0) {
                //获取所有没有分组的客户
                $all_user_ids = SysUser::where('group_id', 0)->where('user_type', '01')->pluck('id');
            }
            if (count($all_user_ids) > 0) {

                foreach ($all_user_ids as $user_id) {
                    foreach ($items as $item) {
                        if ($user_id == 0) {
                            continue;
                        }
                        $model = CustomerProduct::updateOrCreate(
                            [
                                'spec_id' => intval($item['spec_id']),
                                'user_id' => intval($user_id),
                            ],
                            [
                                'product_id' => intval($item['product_id']),
                                'user_price' => floatval($item['user_price']),
                                'channel_group_id' => floatval($item['channel_group_id']),
                                'is_loop' =>  $item['is_loop'] ?? 0,
                                'sort_type' => $params['sort_type'] ?? 1,
                                'timeout' =>  intval($item['timeout']) ?? 0,
                                'submit_order_count' =>  intval($submit_order_count),

                            ]
                        );
                        CustomerProductChannel::where('customer_product_id', $model->id)->delete();
                        if (intval($params['channel_group_id']) == 0) {

                            foreach ($channels as $channel) {
                                $tmp_add = [
                                    'channel_id' => intval($channel['channel_id']),
                                    'submit_nums' => intval($channel['submit_nums']),
                                    'sort' => intval($channel['sort']),
                                    'customer_product_id' => $model->id,
                                ];
                                CustomerProductChannel::create($tmp_add);
                            }
                        }
                    }
                }
            }

            /*
            if(count($items) > 0){
                foreach ($items as $item){

                    foreach ($all_user_ids as $user_id){
                        if($user_id == 0){
                            continue;
                        }
                        $model = CustomerProduct::updateOrCreate(
                            [
                                'spec_id' => intval($item['spec_id']),
                                'user_id' => intval($user_id),
                            ],
                            [
                                'product_id' => intval($item['product_id']),
                                'user_price' => floatval($item['user_price']),
                                'channel_group_id' => floatval($item['channel_group_id']),
                                'is_loop' =>  $item['is_loop'] ?? 0,
                                'sort_type' => $params['sort_type'] ?? 1,
                                'timeout' =>  intval($item['timeout']) ?? 0,
                            ]
                        );
                        CustomerProductChannel::where('customer_product_id',$model->id)->delete();
                        if(intval($params['channel_group_id']) == 0){

                            foreach ($channels as $channel){
                                $tmp_add = [
                                    'channel_id' => intval($channel['channel_id']),
                                    'submit_nums' => intval($channel['submit_nums']),
                                    'sort' => intval($channel['sort']),
                                    'customer_product_id' => $model->id,
                                ];
                                CustomerProductChannel::create($tmp_add);
                            }
                        }
                    }

                }

            }
            */
        });
        return success([], '添加成功');
    }

    public function destroy($ids, Request $request, CustomerProduct $model)
    {
        $id_arr = explode(',', $ids);
        DB::transaction(function () use ($id_arr, $model) {
            $model->destroy($id_arr);
            DB::table('cz_customer_product_channel')->whereIn('customer_product_id', $id_arr)->delete();
        });
    }
}
